package com.gjy.concurrency.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程顺序执行
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-05-17 09:07:58
 */
public class SeqExecTest {

    private static final Logger logger = LoggerFactory.getLogger(SeqExecTest.class);
    static Thread ta;
    static Thread tb;
    static Thread tc;

    public static void main(String[] args) {
    }

    private static void lockSupport() {
        AtomicInteger flag = new AtomicInteger(1);
        ExecutorService pool = Executors.newFixedThreadPool(5);

        ta = new Thread(() -> {
            try {
                if (flag.get() != 1) {
                    LockSupport.park();
                }
                flag.getAndIncrement();
                logger.info("c1 = {}", Thread.currentThread().getName());
                LockSupport.unpark(tb);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        tc = new Thread(() -> {
            try {
                if (flag.get() != 1) {
                    LockSupport.park();
                }
                flag.getAndIncrement();
                logger.info("c3 = {}", Thread.currentThread().getName());
                LockSupport.unpark(ta);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        tb = new Thread(() -> {
            try {
                if (flag.get() != 1) {
                    LockSupport.park();
                }
                flag.getAndIncrement();
                logger.info("c2 = {}", Thread.currentThread().getName());
                LockSupport.unpark(tc);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        ta.start();
        tb.start();
        tc.start();
    }

    private static void object() {
        AtomicInteger flag = new AtomicInteger(1);
        ExecutorService pool = Executors.newFixedThreadPool(5);

        Object lock = new Object();

        pool.submit(() -> {
            synchronized (lock) {
                try {
                    if (flag.get() != 1) {
                        lock.wait();
                    }
                    flag.getAndIncrement();
                    logger.info("c1 = {}", Thread.currentThread().getName());
                    lock.notifyAll();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        pool.submit(() -> {
            synchronized (lock) {
                try {
                    if (flag.get() != 2) {
                        lock.wait();
                    }
                    flag.getAndIncrement();
                    logger.info("c2 = {}", Thread.currentThread().getName());
                    lock.notifyAll();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        pool.submit(() -> {
            synchronized (lock) {
                try {
                    if (flag.get() != 3) {
                        lock.wait();
                    }
                    flag.getAndIncrement();
                    logger.info("c3 = {}", Thread.currentThread().getName());
                    lock.notifyAll();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        pool.shutdown();
    }

    private static void lock() {
        Lock l = new ReentrantLock();
        Condition c1 = l.newCondition();
        Condition c2 = l.newCondition();
        Condition c3 = l.newCondition();

        AtomicInteger flag = new AtomicInteger(1);

        ExecutorService pool = Executors.newFixedThreadPool(5);

        pool.submit(() -> {
            l.lock();
            try {
                if (flag.get() != 1) {
                    c1.await();
                }
                c2.signal();
                flag.getAndIncrement();
                logger.info("c1 = {}", Thread.currentThread().getName());
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                l.unlock();
            }
        });

        pool.submit(() -> {
            l.lock();
            try {
                if (flag.get() != 2) {
                    c1.await();
                }
                c3.signal();
                flag.getAndIncrement();
                logger.info("c2 = {}", Thread.currentThread().getName());
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                l.unlock();
            }
        });

        pool.submit(() -> {
            l.lock();
            try {
                if (flag.get() != 3) {
                    c1.await();
                }
                c1.signal();
                flag.getAndIncrement();
                logger.info("c3 = {}", Thread.currentThread().getName());
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                l.unlock();
            }
        });

        pool.shutdown();
    }
}
